package com.example.utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;

public class MD5Util {
    //生成6位随机字符串的盐，由数字、大小写字母组成
    public static String getSaltString(){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<6;i++){
            int num=random.nextInt(str.length());
            sb.append(str.charAt(num));
        }
        return sb.toString();
    }

    //把密码 + 盐 ，一同经过MD5加密
    public static String toMD5String(String str,String salt){
        try{
            return Hex.encodeHexString(DigestUtils.md5(str+salt));
        }catch(Exception e){
            return null;
        }
    }
}

